package com.bwie.es.sync;

import com.bwie.common.domain.Shop;
import com.bwie.common.result.Result;
import com.bwie.es.feign.GoodsFeign;
import com.bwie.es.service.EsService;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Log4j2
public class EsSync {
    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private EsService esService;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void esList(){
        Result<List<Shop>> list = goodsFeign.list();
        List<Shop> data = list.getData();
        if(!Collections.isEmpty(data)){
            log.info("开始同步,共{}条数据",data.size());

            long l = System.currentTimeMillis();

            esService.esAdd(data);

            log.info("同步结束,共耗时{}毫秒",System.currentTimeMillis()-l);
        }
    }

}
